<?php

namespace Home\Controller;

/**
 * Description of IndexController
 *
 * @author 阿明 symphp@foxmail.com
 */
class IndexController extends \Think\Controller {

    private $_model = null;

    protected function _initialize() {
        //构建数据
        $meta_titles = array(
            'index' => '京西商城',
            'add' => '商品添加',
            'edit' => '商品修改',
            'delete' => '商品删除',
        );
        $meta_title = $meta_titles[ACTION_NAME];
        //发送数据
        $this->assign('meta_title', $meta_title);
        //实例化类
        $this->_model = D('Article');
        //获取一个数据缓存保存到变量
        $categories = S('goods_categories');
        //如不存在该缓存即创建
        if (empty($categories)) {
            //获取发送分类列表
            $categories = D('goodsCategory')->getList();
            S('goods_categories', $categories);
        }
        $this->assign('categorie', $categories);

        //获取帮助分类列表
        $getHelpArticleList = S('getHelpArticleList');
        if (empty($getHelpArticleList)) {
            $getHelpArticleList = $this->_model->getHelpArticleList();
            S('getHelpArticleList', $getHelpArticleList);
        }
        $this->assign('getHelpArticleList', $getHelpArticleList);
        
        //首页才展示分类列表
        if(ACTION_NAME == 'index'){
            $this->assign('show_category', true);
        }else{
            $this->assign('show_category', false);
        }
    }

    public function login() {
        if(IS_POST){
            //收集数据
            if($this->_model->create('','login') === false){
                $this->error(get_error($this->_model->getError()));
            };
            //登录验证
            $memberModel = D('member');
            if($memberModel->addlogin() === false){
                 $this->error(get_error($memberModel->getError()));
            }
            //添加成功
            $this->success('登录成功',U('index/index'),2);
        }else{
        $this->display('login');
        }
    }

    /**
     * 显示首页
     */
    public function index() {
        //获取数据
        $goods_model = D('goods');
        //获取商品状态为X的值
        $goods_list['best_list'] = $goods_model->getGoodsListByGoodsStatus(1);
        $goods_list['new_list'] = $goods_model->getGoodsListByGoodsStatus(2);
        $goods_list['not_list'] = $goods_model->getGoodsListByGoodsStatus(4);
        //发送数据
        $this->assign($goods_list);
        $this->display();
    }
    
    /**
     * 获取商品详情
     * @param type $id
     */
    public function goods($id){
        $goods_model = D('Goods');
        $row = $goods_model->getGoodsInfo($id);
        $this->assign('row',$row);
        $this->assign('gallerys',$row['galleries']);
        $this->display();
    }

}
